/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ForumsViewPanel.java
 *
 * Created on 16/04/2011, 13:10:23
 */

package presentation.gui;

import common.forum.items.ThreadInfo;
import domain.ClientController;
import java.util.Vector;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author 
 */
public class ThreadsViewPanel extends javax.swing.JPanel {
    private  DefaultListModel _threadsList;
    private ClientController _clientController;
    private int _threadsIndexSelected;
    private String _ForumId;
    private StartWindow _start;
     DefaultListModel thread_list;

    /** Creates new form ForumsViewPanel */
    public ThreadsViewPanel(ClientController clientController, String forumId, StartWindow start) {
        _clientController= clientController;
        _threadsList= new DefaultListModel();
        _ForumId= forumId;
        _start=start;
       this.fillListThreads();
        setVisible(true);
        initComponents();
    }

    public void fillListThreads( ) {
         Vector <ThreadInfo> threads= _clientController.getThreadsList(_ForumId);
        //this.setVisible(true);
     for (int j=0; j<threads.size(); j++  ){
               getThreadsListModel().addElement(threads.get(j).getTitle());
             }
    }

    private StartWindow getStartWindow(){
        return this._start;
    }

    public   DefaultListModel  getThreadsListModel() {
        return _threadsList;
    }

    public ClientController getClientController(){
        return this._clientController;
    }
    
    private String getForumId(){
    	return this._ForumId;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane4 = new javax.swing.JLayeredPane();
        jButton2 = new javax.swing.JButton();
        jLayeredPane2 = new javax.swing.JLayeredPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jButton4 = new javax.swing.JButton();

        setLayout(null);

        jLayeredPane4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Forum System", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Kristen ITC", 1, 14), new java.awt.Color(255, 255, 255))); // NOI18N

        jButton2.setFont(new java.awt.Font("Kristen ITC", 1, 14)); // NOI18N
        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/presentation/gui/pics/view_bottom.png"))); // NOI18N
        jButton2.setText("Add Thread");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jButton2.setBounds(270, 490, 170, 40);
        jLayeredPane4.add(jButton2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Please choose one of the Threads below", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.ABOVE_TOP, new java.awt.Font("Kristen ITC", 1, 14), new java.awt.Color(255, 255, 255))); // NOI18N

        jList1.setFont(new java.awt.Font("Kristen ITC", 1, 14));
        jList1.setModel(getThreadsListModel());
        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList1ValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);

        jScrollPane1.setBounds(10, 30, 670, 410);
        jLayeredPane2.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane2.setBounds(10, 30, 690, 450);
        jLayeredPane4.add(jLayeredPane2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/presentation/gui/pics/go_back.png"))); // NOI18N
        jButton4.setBorder(null);
        jButton4.setBorderPainted(false);
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jButton4.setBounds(20, 20, 40, 30);
        jLayeredPane4.add(jButton4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        add(jLayeredPane4);
        jLayeredPane4.setBounds(10, 11, 719, 548);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // Back Button
        this.setVisible(false);
        getStartWindow().nonDisplayForum(); //setVisible false
        getStartWindow().getForum().displayForum( new ForumsViewPanel( getClientController(), getStartWindow()));
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    // Add Thread Button
       getStartWindow().nonDisplayForum(); //setVisible false
       getStartWindow().getForum().displayForum( new AddThreadPanel(getClientController(),_ForumId, getStartWindow()));
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList1ValueChanged
          //View Thread Button
      //  this.thread_list=   (DefaultListModel)jList1.getModel();
         _threadsIndexSelected= jList1.getSelectedIndex();
         String threadIdString=  Integer.toString(_threadsIndexSelected);
         if (_threadsIndexSelected>=0  ){
             Vector <ThreadInfo> threads= _clientController.getThreadsList(_ForumId);
             int thread_id=threads.get(_threadsIndexSelected).getThread_id();
             String thread_id_string=  Integer.toString(thread_id);
              getStartWindow().nonDisplayForum(); //setVisible false
             getStartWindow().getForum().displayForum( new PostsViewPanel( getClientController(),getForumId(),thread_id_string, getStartWindow()));
        }
    }//GEN-LAST:event_jList1ValueChanged


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton4;
    private javax.swing.JLayeredPane jLayeredPane2;
    private javax.swing.JLayeredPane jLayeredPane4;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}
